package com.dayuanit.dy17.atm.atmbackoffice.controller;

import com.dayuanit.dy17.atm.atmbackoffice.dto.ResponseDTO;
import com.dayuanit.dy17.atm.atmbackoffice.service.ISalaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class SalaryController extends BaseController {

    @Autowired
    private ISalaryService salaryService;

    @RequestMapping("/salary/uploadSalary")
    public ResponseDTO uploadSalary(String remark, MultipartFile salaryExcelFile) {
        System.out.println("remark="+ remark);
        try {
            String fileName = UUID.randomUUID().toString().replace("-", "");
            salaryExcelFile.transferTo(new File("D:/dayuanit/17/tmp/" + fileName));

            salaryService.createSalaryOrder(getUserId(), fileName, remark);
            return ResponseDTO.success();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return ResponseDTO.fail("上传失败");
        }
    }

    @RequestMapping("/salary/loadSalaryOrder")
    public ResponseDTO loadSalaryOrder() {
        return ResponseDTO.success(salaryService.loadSalaryOrder());
    }

    @RequestMapping("/salary/loadSalaryDetail")
    public ResponseDTO loadSalaryDetail(long salaryId) {
        return ResponseDTO.success(salaryService.querySalaryDetail(salaryId));
    }


    @RequiresRoles("CFO")
    @RequestMapping("/salary/approval")
    public ResponseDTO approval(long salaryId, int approvalStatus) {
        salaryService.approval(salaryId, approvalStatus);
        return ResponseDTO.success();
    }
}
